package org.osami.backoffice.eventswitch;

import org.mule.transformers.AbstractTransformer;
import org.mule.umo.transformer.TransformerException;
import org.ow2.jasmine.event.beans.JasmineEventNotification;

import java.io.Serializable;
import java.util.Date;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: guillaume
 * Date: 19/08/11
 * Time: 11:56
 * To change this template use File | Settings | File Templates.
 */
public class AlarmMapTransformer extends AbstractTransformer {

    public AlarmMapTransformer() {
        this.registerSourceType(Map.class);
        this.setReturnClass(JasmineEventNotification.class);
    }

    @Override
    protected Object doTransform(Object src, String encoding) throws TransformerException {
        Map<String, Serializable> map = (Map<String, Serializable>) src;

        String id = (String) map.get("alarm.id");
        Date date = (Date) map.get("alarm.date");
        String message = (String) map.get("alarm.message");
        String severity = (String) map.get("alarm.severity");

        // details
        String source = (String) map.get("source");
        String type = (String) map.get("type");
        Double threshold = (Double) map.get("threshold");
        Double value = (Double) map.get("value");

        // Create the event
        JasmineEventNotification event = new JasmineEventNotification();
        event.setRule(type);
        event.setId(id);
        event.setDate(date);
        event.setLevel(severity);
        event.setMessage(message);
        event.addDetail("source", source);
        event.addDetail("threshold", String.valueOf(threshold));
        event.addDetail("value", String.valueOf(value));
        return event;
    }
}
